package org.chuan.hisports.controller;

import org.chuan.hisports.entity.CUList;
import org.chuan.hisports.entity.CoachInfo;
import org.chuan.hisports.entity.Scheme;
import org.chuan.hisports.entity.UserInfo;
import org.chuan.hisports.service.CoachService;
import org.chuan.hisports.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("coach")
public class CoachController {
    @Autowired
    CoachService coachService;

    //注册，仅通过管理员账号注册，因此仅需要手机账号和密码
    @RequestMapping("register")
    @ResponseBody
    public String register(@RequestParam("phone") String phone,
                           @RequestParam("password") String password){
        return null;
    }

    //登录
    @RequestMapping("login")
    @ResponseBody
    public JSONResult login(@RequestParam("phone") String phone,
                            @RequestParam("pasword") String password,
                            HttpServletRequest request){
        CoachInfo coachInfo = coachService.login(phone, password);
        //登录失败，返回信息
        if(coachInfo == null){
            return JSONResult.build(200,"登录失败，请查看账号密码是否正确",null);

        }
        HttpSession hs = request.getSession();
        hs.setAttribute("cid",coachInfo.getCid());
        hs.setAttribute("name",coachInfo.getName());
        return JSONResult.build(200,"success",null);
    }

    //更新教练个人信息
    @RequestMapping("updateinfo")
    @ResponseBody
    public String updateinfo(@RequestParam("cid") String cid){

        return "success";
    }
    //查询教练的所有申请的成员
    @RequestMapping("selectApply")
    @ResponseBody
    public List selectApply(String uid){
        List<CUList> list = coachService.selectApply(uid);
        return list;
    }
    //查询教练旗下所有申请通过的会员
    @RequestMapping("selectAll")
    @ResponseBody
    public JSONResult selectAll(@RequestParam("cid") String cid){
        List<UserInfo> list = coachService.selectUserByCid(cid);
        return JSONResult.build(200,"success",list);

    }

    //更新会员申请状态（同意或拒绝）

    @RequestMapping("updatesta")
    @ResponseBody
    public List<CUList> updatesta(String cid,String uid,String status){
        return null;
    }

    //编写会员健身方案
    @RequestMapping("insertScheme")
    @ResponseBody
    public JSONResult insertScheme(@RequestParam("uid") String uid,
                               @RequestParam("walking") String walking,
                               @RequestParam("runing") String runing,
                               @RequestParam("bike") String bike,
                               @RequestParam("training") String training){
        coachService.insertToUid(uid, walking, runing, bike, training);
        return JSONResult.build(200,"success",null);
    }

    //更新会员健身方案
    @RequestMapping("updateScheme")
    @ResponseBody
    public JSONResult updateScheme(@RequestParam("uid") String uid,
                               @RequestParam("walking") String walking,
                               @RequestParam("runing") String runing,
                               @RequestParam("bike") String bike,
                               @RequestParam("training") String training){
        coachService.updataScheme(uid, walking, runing, bike, training);

        return JSONResult.build(200,"success",null);
    }

    //查看会员的健身方案
    @RequestMapping("readScheme")
    @ResponseBody
    public JSONResult readScheme(@RequestParam("uid") String uid)
    {
        Scheme scheme = coachService.selectByUid(uid);
        return JSONResult.build(200,"success",scheme);
    }

    //查看会员的个人信息
    @RequestMapping("readUserinfo")
    @ResponseBody
    public JSONResult readUserinfo(@RequestParam("cid") String cid){
        List<UserInfo> list = coachService.selectUserByCid(cid);
        return JSONResult.build(200,"success",list);

    }

    //查看教练的详细信息
    @RequestMapping("readCoachinfo")
    @ResponseBody
    public JSONResult readCoachinfo(@RequestParam("cid") String cid){
        CoachInfo coachInfo = coachService.selectByCid(cid);
        return JSONResult.build(200,"success",coachInfo);
    }
//更新教练的详细信息
    @RequestMapping("updataCoachinfo")
    @ResponseBody
    public JSONResult updataCoachinfo(@RequestParam("cid") String cid,
                                      @RequestParam("name") String name,
                                      @RequestParam("slogan") String slogan,
                                      @RequestParam("able") String able,
                                      @RequestParam("tyear") String tyear,
                                      @RequestParam("weight") String weight,
                                      @RequestParam("height") String height,
                                      @RequestParam("age") String age){
        coachService.updataInfo(cid, name, slogan, able, tyear, weight, height, age);


        return JSONResult.build(200,"success",null);
    }
    //更新图片
    @RequestMapping("updataPicture")
    @ResponseBody
    public JSONResult updataPicture(@RequestParam("cid") String cid){
        return null;
    }
    //页面跳转
    @RequestMapping("toindex")
    public String toindex(){
        return "coachs/coach";
    }
    @RequestMapping("tomember")
    public String tomember(){
        return "coachs/coachmember";
    }
    @RequestMapping("toinfo")
    public String toinfo(){
        return "coachs/coachinfo";
    }
}
